import { createApp } from 'vue'
import LoginExpiredModal from '@/components/LoginExpiredModal.vue'

class LoginExpiredManager {
    constructor() {
        this.modalInstance = null
        this.isShowing = false
    }

    // 显示登录失效提示
    show() {
        // 防止重复显示
        if (this.isShowing) {
            return
        }

        this.isShowing = true

        // 创建弹窗容器
        const container = document.createElement('div')
        container.id = 'login-expired-modal-container'
        document.body.appendChild(container)

        // 创建Vue应用实例
        const app = createApp(LoginExpiredModal)
        this.modalInstance = app.mount(container)

        // 显示弹窗
        this.modalInstance.show()

        // 监听弹窗关闭事件
        const originalHide = this.modalInstance.hide
        this.modalInstance.hide = () => {
            originalHide.call(this.modalInstance)
            this.cleanup()
        }
    }

    // 清理资源
    cleanup() {
        if (this.modalInstance) {
            // 延迟清理，确保动画完成
            setTimeout(() => {
                const container = document.getElementById('login-expired-modal-container')
                if (container) {
                    document.body.removeChild(container)
                }
                this.modalInstance = null
                this.isShowing = false
            }, 300)
        }
    }

    // 隐藏弹窗
    hide() {
        if (this.modalInstance) {
            this.modalInstance.hide()
        }
    }
}

// 创建单例实例
const loginExpiredManager = new LoginExpiredManager()

export default loginExpiredManager
